from django.shortcuts import render_to_response, get_object_or_404
from django.http import HttpResponseRedirect, Http404, HttpResponsePermanentRedirect
from django.contrib.auth.decorators import login_required
from django.template import RequestContext
from django.contrib.auth.models import User
from django.template.defaultfilters import slugify
from django.views.generic.list_detail import object_list

from models import Forum, Topic, Post
from forms import AddGenForm, AddPostForm, UserProfileForm, UserForm

import sha, datetime, random

def render_with_request(request, template, context={}):
	return render_to_response(template, context, context_instance=RequestContext(request))

def update_new_post(new_post, topic):
	topic.latest_post = new_post
	topic.post_count += 1
	topic.forum.latest_post = new_post
	topic.forum.post_count += 1
	topic.forum.save()
	return topic.save()

def new_forum(request):
	context = {}
	if request.POST:
		form = AddGenForm(request.POST)
		if form.is_valid():
			new_forum = Forum(**{'description': form.clean_data['description'], 'title': form.clean_data['title'], 'slug': slugify(form.clean_data['title']),})
			new_forum.save()
		else:
			context['form'] = form
	else:
		context['form'] = AddGenForm()

	return render_with_request(request, 'beastly/forum_new.html', context)

def topic_list(request, forum_id):
	if request.GET:
		page_number = request.GET.get('page', 1)
	else:
		page_number = 1

	forum = get_object_or_404(Forum, id=forum_id)
	return object_list(request, queryset=forum.topic_set.all(), paginate_by=10, template_name='beastly/topics.html', template_object_name='topic', allow_empty=True, extra_context={'forum': forum,}, page=page_number)	

def new_topic(request, forum_id):
	forum = get_object_or_404(Forum, id=forum_id)
	context = {'forum': forum}

	if request.POST:
		form = AddGenForm(request.POST)
		if form.is_valid():
			new_topic = Topic(**{'title':form.clean_data['title'], 'slug': slugify(form.clean_data['title']), 'forum': forum,})
			new_topic.save()
			new_post = Post(**{'description': form.clean_data['description'], 'topic': new_topic, 'user': request.user,})
			new_post.save()
			update_new_post(new_post, new_topic)	

			return HttpResponseRedirect('/beastly/forums/' + forum_id)
		else:
			context['form'] = form
	else:
		context['form'] = AddGenForm()

	return render_with_request(request, 'beastly/topic_new.html', context)
	
def post_list(request, forum_id, topic_id):
	topic = get_object_or_404(Topic, id=topic_id)
	topic.views += 1
	topic.save()
	context = {'topic': topic, 'forum': topic.forum,}

	request.session['topics_viewed'] = {} 
	request.session['topics_viewed'][int(forum_id)] = {} 
	request.session['topics_viewed'][int(forum_id)][int(topic_id)] = True

	if request.GET:
		page_number = request.GET.get('page', 1)
	else:
		page_number = 1

	if request.POST:
		form = AddPostForm(request.POST)
		if form.is_valid():
			new_post = Post(**{'description': form.clean_data['description'], 'topic_orig': topic, 'user': request.user, 'count': topic.post_count + 1})
			new_post.save()
			update_new_post(new_post, topic)
			return HttpResponseRedirect('/beastly/forums/' + forum_id + '/topics/' + topic_id + '/?page=' + str(page_number))
		else:
			context['form'] = form
	else:
		context['form'] = AddGenForm()

	return object_list(request, queryset=Post.objects.filter(topic=topic), paginate_by=10, template_name='beastly/posts.html', template_object_name='post', allow_empty=True, extra_context=context, page=page_number)	

def accounts_profile(request):
        return HttpResponsePermanentRedirect('/beastly/')

def users(request):
	user_list = list(User.objects.all())
	return render_with_request(request, 'beastly/users.html', {'user_list': user_list})

def settings(request):
	if request.POST:
		user_form = UserForm(request.POST)
		user_profile_form = UserProfileForm(request.POST)
	else:
		user_form = UserForm(request.user)
		user_profile_form = UserProfileForm(request.user.userprofile)

	context = {'user_form': user_form, 'user_profile_form': user_profile_form}
	return render_with_request(request, 'beastly/settings.html', context)
